import java.io.*;

 /**
  * Roxanne O'Brien
  * COSC 603
  * Project 1
  * February 18, 2012.
  */

/**
 * The Class RoxanneStutter.
 * This class will read in a text file and check for any repeated words. 
 */
public class RoxanneStutter
{

	/**
	 * The main method.
	 * This method will read a file into the program
	 * This method will also go through the file line by line and look for any repeated words.
	 * This method will also use string split and create an array for the line.
	 * @param args the arguments
	 */
	public static void main(String[] args) 
	{
		String strLine;
		String temp = "";
		int count = 1;
		
		 try{
			   // Open the file that is the first 
			   // command line parameter
			   FileInputStream fstream = new FileInputStream(args[0]);
			   // Get the object of DataInputStream
			   DataInputStream in = new DataInputStream(fstream);
			   BufferedReader br = new BufferedReader(new InputStreamReader(in));
			
			   //Read File Line By Line
			   
			   while ((strLine = br.readLine()) != null)
				{
					String[] strLine1;
					String delimiter = "[:;( .,!?\"-]+\\s*";
					strLine1 = strLine.split(delimiter);
					
					for (int i = 0; i < strLine1.length; i++)   
					{
						//Check for last word in sentence
						if (i+1 > strLine1.length - 1)
						{
							temp = strLine1[i];
							break;		
						}

						if (strLine1[i].equals(strLine1[i+1]))
						{
							System.out.println("Repeated word on line " + count + ": " + strLine1[i] + " " + strLine1[i+1]);
						}

						if (temp.isEmpty() == false)
						{
							if (strLine1[i].equals(temp))
							{
								System.out.println("Repeated word on line " + count + ": " + strLine1[i] + " " + temp);
							}
						}
					}
					
					//Increment to the next line
					count++;
				}
			   //Close the input stream
			   in.close();
			   
		}catch (Exception e){//Catch exception if any
			   System.err.println("Error: " + e.getMessage());
			   
	}		   
}
	
}


